package com.dianzi.certification.controller;

import com.dianzi.certification.vo.R;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author copa
 * @createDate 2022-03-09 10:39
 * @function 错误页面处理
 */
@RestController
public class HttpErrorController implements ErrorController {

    private final static String ERROR_PATH = "/error";

    @ResponseStatus(code= HttpStatus.BAD_REQUEST,reason="bad request")
    @RequestMapping(path  = ERROR_PATH )
    public R error400(){
        return R.errorCodeMsg(400, "该状态码表示请求报文存在语法错误，需要对请求内容进行修改后再次进行请求~");
    }

    @ResponseStatus(code= HttpStatus.UNAUTHORIZED,reason="unauthorized")
    @RequestMapping(path  = ERROR_PATH )
    public R error401(){
        return R.errorCodeMsg(401, "若之前没有进行请求，则表示客户端发送的请求需要包含通过HTTP认证的认证信息；\n若之前已经进行过请求即服务端已经将包含HTTP认证的认证信息发给过客户端，然后客户端提交认证信息后服务端继续返回状态码401则表示用户认证失败~");
    }

    @ResponseStatus(code= HttpStatus.FORBIDDEN,reason="server error")
    @RequestMapping(path  = ERROR_PATH )
    public R error403(){
        return R.errorCodeMsg(403, "未获得文件系统访问权限~\n使用受限制的IP地址访问网站资源~\n");
    }

    @ResponseStatus(code= HttpStatus.NOT_FOUND,reason="not found")
    @RequestMapping(path  = ERROR_PATH )
    public R error404(){
        return R.errorCodeMsg(404, "服务器上找不到请求的资源【找到了服务器，却没有找到对应服务器的资源地址】~\n拒绝请求但不想向客户端表明原因~");
    }

    @ResponseStatus(code= HttpStatus.UNSUPPORTED_MEDIA_TYPE,reason="unsupported media type")
    @RequestMapping(path  = ERROR_PATH )
    public R error415(){
        return R.errorCodeMsg(415, "服务器无法处理请求附带的媒体格式~");
    }

    @ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR,reason="server error")
    @RequestMapping(path  = ERROR_PATH )
    public R error500(){
        return R.errorCodeMsg(500, "服务器端在执行请求时发生了错误~\nWeb应用存在bug或者出现临时故障~");
    }

    @ResponseStatus(code= HttpStatus.BAD_GATEWAY,reason="bad gateway")
    @RequestMapping(path  = ERROR_PATH )
    public R error502(){
        return R.errorCodeMsg(502, "bad gateway");
    }

    @ResponseStatus(code= HttpStatus.SERVICE_UNAVAILABLE,reason="service unavailable")
    @RequestMapping(path  = ERROR_PATH )
    public R error503(){
        return R.errorCodeMsg(502, "服务器此时无法处理请求（服务暂时不可用）~\n服务器暂时处于超负荷状态\n服务器正进行停机维护");
    }
}